//
//  CaseQuestionView.m
//  MobileStudy
//
//  Created by cuihongbao on 13-7-25.
//  Copyright (c) 2013年 hongbao.cui. All rights reserved.
//

#import "CaseQuestionView.h"
#import <QuartzCore/QuartzCore.h>
@implementation CaseQuestionView
@synthesize cqdelegate;
@synthesize isShowAnswer_;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame  isSingle:(BOOL)isSingle isShowAnswer:(BOOL)isShowAnswer  anlysise:(BOOL )anlysise dataDict:(NSMutableDictionary *)dataDict_;
{
    self = [super initWithFrame:frame];
    if (self) {
        FMSCHBLog(@"=====dataDict_%@",dataDict_);
        // Initialization code
        NSString *orderIdStr =[dataDict_ objectForKey:@"orderid"];
        NSString *titleStr =[dataDict_ objectForKey:@"content"];
        NSString *str = [NSString stringWithFormat:@"%@.%@",orderIdStr,titleStr];
        NSString *has_sub = [dataDict_ objectForKey:@"has_sub"];
        CGSize size_0 = CGSizeMake(FMSCREENWIDTH-20, 1000);
        CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:14.0]constrainedToSize:size_0 lineBreakMode:NSLineBreakByWordWrapping];
        int x,y,w,h;
        x = 10;y=2;w=FMSCREENWIDTH-20;h = size.height;//400
        UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        //        UIImage *bgImage = [[ApplicationInfo getImageFromLocalFile:@"bgImage"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
        //        [bgImageView setImage:bgImage];
        [bgImageView setTag:666];
        [bgImageView setBackgroundColor:[UIColor colorWithRed:213/255.0 green:222/255.0 blue:235/255.0 alpha:11.0]];
        bgImageView.layer.cornerRadius = 2.0;
        bgImageView.layer.borderWidth = 1.0;
        bgImageView.layer.borderColor = [UIColor colorWithRed:213/255.0 green:222/255.0 blue:235/255.0 alpha:11.0].CGColor;
        [self addSubview:bgImageView];
        
        UIWebView *selectionView = [[UIWebView alloc] initWithFrame:CGRectMake(x,y,w-20,h)];
        [selectionView setBackgroundColor:[UIColor clearColor]];
        [selectionView setOpaque:NO];
        [selectionView setTag:667];
        [selectionView setDelegate:self];
        NSString *HTMLData = str;
        NSString *path = [FMPAPERSAVE_PATH stringByAppendingPathComponent:@"image"];
        
        [selectionView loadHTMLString:HTMLData baseURL:[NSURL fileURLWithPath:path]];
        [bgImageView addSubview:selectionView];
        
        NSString *aString = [dataDict_ objectForKey:@"option_a"];
        NSString *bString = [dataDict_ objectForKey:@"option_b"];
        NSString *cString = [dataDict_ objectForKey:@"option_c"];
        NSString *dString = [dataDict_ objectForKey:@"option_d"];
        NSString *eString = [dataDict_ objectForKey:@"option_e"];
        NSString *fString = [dataDict_ objectForKey:@"option_f"];
        NSMutableString *questionStr = [NSMutableString stringWithCapacity:1];
        if (![aString isEqualToString:@"(null)"]) {
            [questionStr appendFormat:@"<p>1.%@</p>",aString];
        }
        if (![bString isEqualToString:@"(null)"]) {
            [questionStr appendFormat:@"<p>2.%@</p>",bString];
        }
        if (![cString isEqualToString:@"(null)"]) {
            [questionStr appendFormat:@"<p>3.%@</p>",cString];
        }
        if (![dString isEqualToString:@"(null)"]) {
            [questionStr appendFormat:@"<p>4.%@</p>",dString];
        }
        if (![eString isEqualToString:@"(null)"]) {
            [questionStr appendFormat:@"<p>5.%@</p>",eString];
        }
        if (![fString isEqualToString:@"(null)"]) {
            [questionStr appendFormat:@"<p>6.%@</p>",fString];
        }
//        size = [questionStr sizeWithFont:[UIFont systemFontOfSize:14.0]constrainedToSize:size_0 lineBreakMode:UILineBreakModeWordWrap];
        if ([aString isEqualToString:@"(null)"]&&[bString isEqualToString:@"(null)"]&&[cString isEqualToString:@"(null)"]&&[dString isEqualToString:@"(null)"]&&[eString isEqualToString:@"(null)"]&&[fString isEqualToString:@"(null)"]) {
            //没有问题m
        }else{
            UIWebView *questionWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
            [questionWebView setOpaque:NO];
            [questionWebView setTag:668];
            [questionWebView setDelegate:self];
            [questionWebView setFrame:CGRectMake(x, bgImageView.frame.origin.y, FMSCREENWIDTH-20, 400)];
            [questionWebView loadHTMLString:questionStr baseURL:[NSURL fileURLWithPath:path]];
            [questionWebView setBackgroundColor:[UIColor clearColor]];
            [self addSubview:questionWebView];
        }
//        y =  y+43+43*[optionsArray count];
        UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, FMSCREENWIDTH-20, 1)];
        [lineLabel setBackgroundColor:[UIColor lightGrayColor]];
        [lineLabel  setTag:670];
        if (isShowAnswer) {
            [lineLabel setHidden:NO];
        }else{
            [lineLabel setHidden:YES];
//            [self setContentSize:CGSizeMake(300,h+[optionsArray count]*43)];
        }
        [self addSubview:lineLabel];
        x = x+5;
        y = y+1+5;
        NSString *user_answer = [dataDict_ objectForKey:@"user_answer"];
        NSString *answer = [dataDict_ objectForKey:@"answer"];
        if ([user_answer isEqualToString:answer]) {

        }else{
            if ([user_answer isEqualToString:@""]) {
                user_answer = @"您没有选择答案";
            }
        }
        NSString *anyliseHTMLData = [NSString stringWithFormat:@"<p><a><font color=\"#0000FF\">您的答案:</font>%@     </a></p><p><a><font color=\"#0000FF\">知识点:</font>%@</a></p><p><a><font color=\"#0000FF\">正确答案:</font>%@</a></p><a><font color=\"#0000FF\">试题分析:</font> %@</a>",@"",[dataDict_ objectForKey:@"category_name"],[dataDict_ objectForKey:@"answer"],[dataDict_ objectForKey:@"analysis"]];
        CGSize anyliseSize = [anyliseHTMLData sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(FMSCREENWIDTH-20, 1000)];
        x = x-5;
        y = y;
        UIWebView *anylisewebView = [[UIWebView alloc] initWithFrame:CGRectMake(x,y,anyliseSize.width,anyliseSize.height+60)];
        [anylisewebView setBackgroundColor:[UIColor clearColor]];
        [anylisewebView setTag:669];
        if (isShowAnswer) {
            [anylisewebView setHidden:NO];
            isShowAnswer_ = YES;
            UIWebView *questionWebView669 = (UIWebView *)[self viewWithTag:669];
            [self setContentSize:CGSizeMake(FMSCREENWIDTH, 2+questionWebView669.frame.origin.y+questionWebView669.frame.size.height)];
        }else{
            isShowAnswer_ = NO;
            [anylisewebView setHidden:YES];
        }
        [anylisewebView setUserInteractionEnabled:NO];
        [anylisewebView setOpaque:NO];
        [anylisewebView setDelegate:self];
        NSURL *url = [NSURL fileURLWithPath:path];
        [anylisewebView loadHTMLString:anyliseHTMLData baseURL:url];
        [self addSubview:anylisewebView];

    }
    return self;
}
-(void)showAnswer{
    UIWebView *questionWebView668 = (UIWebView *)[self viewWithTag:668];
    UILabel *line = (UILabel *)[self viewWithTag:670];
    [line setHidden:NO];
    UIWebView *questionWebView669 = (UIWebView *)[self viewWithTag:669];
    [questionWebView669 setHidden:NO];
    [self setContentSize:CGSizeMake(FMSCREENWIDTH, 2+questionWebView669.frame.origin.y+questionWebView669.frame.size.height)];
}
- (void)webViewDidFinishLoad:(UIWebView *)_webView
{
// finished loading, hide the activity indicator in the status bar
    UIWebView *questionWebView = (UIWebView *)[self viewWithTag:667];
    questionWebView.frame = CGRectMake(questionWebView.frame.origin.x, questionWebView.frame.origin.y, questionWebView.frame.size.width, questionWebView.scrollView.contentSize.height);
    UIImageView *bgIMageView = (UIImageView *)[self viewWithTag:666];
    [bgIMageView setFrame:CGRectMake(10, 2, FMSCREENWIDTH-20, questionWebView.scrollView.contentSize.height)];
    
    UIWebView *questionWebView668 = (UIWebView *)[self viewWithTag:668];
    questionWebView668.frame = CGRectMake(questionWebView668.frame.origin.x, questionWebView.frame.origin.y+questionWebView.frame.size.height, questionWebView668.frame.size.width, questionWebView668.scrollView.contentSize.height);
    
    [self setContentSize:CGSizeMake(FMSCREENWIDTH, 2+questionWebView668.frame.origin.y+questionWebView668.frame.size.height)];
    
    UILabel *line = (UILabel *)[self viewWithTag:670];
    [line setFrame:CGRectMake(10, questionWebView668.frame.origin.y+questionWebView668.frame.size.height, FMSCREENWIDTH-20, 1)];
//
    UIWebView *questionWebView669 = (UIWebView *)[self viewWithTag:669];
    questionWebView669.frame = CGRectMake(questionWebView669.frame.origin.x, questionWebView.frame.origin.y+questionWebView.scrollView.contentSize.height+questionWebView668.frame.size.height, questionWebView669.frame.size.width, questionWebView669.scrollView.contentSize.height);
    if (isShowAnswer_) {
        [self setContentSize:CGSizeMake(FMSCREENWIDTH, 2+questionWebView669.frame.origin.y+questionWebView669.frame.size.height)];
    }else{
        [self setContentSize:CGSizeMake(FMSCREENWIDTH, 2+bgIMageView.frame.origin.y+bgIMageView.frame.size.height)];
    }
    
//    if (cqdelegate&&[cqdelegate respondsToSelector:@selector(webViewDidFinishLoading:Y:)]) {
//        [cqdelegate webViewDidFinishLoading:self Y:0];
//    }
}
-(void)dealloc{
    cqdelegate = nil;
    //清除UIWebView的缓存
    [[NSURLCache  sharedURLCache ] removeAllCachedResponses];
}
@end
